/*
 * @Author: Zeng Hugh
 * @Date: 2020-06-05 13:32:50
 * @Last Modified by: Zeng Hugh
 * @Last Modified time: 2020-06-03 15:32:57
 */

using RuleJudge.RuleData;
using SLANCommon;
using SLANCommon.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Media;

namespace RuleJudge.Judge
{
    public class Flu_2019nCoV : BaseJudge
    {
        /// <summary>
        /// 构造函数，需要定义项目类型，管，通道信息
        /// </summary>

        #region 定义判读中常量Cutoff,高中低，ΔCt，初始荧光信号强度范围，终点荧光限号强度范围
        //定义常量：Flu-A,Flu-B阳性判断参考值，2019nCoV的ORFab1,N基因阳性判断参考值，内参阳性判断参考值
        public const double FluA_Positive_Cutoff = 35.46;    //Flu-A阳性参考值
        public const double FluB_Positive_Cutoff = 35.42;    //Flu-B阳性参考值
        public const double ORFab1_Positive_Cutoff = 37.04;  //OFRab1阳性参考值
        public const double Ngene_Positive_Cutoff = 36.96;   //N基因阳性参考值
        public const double IC_Positive_Cutoff = 35.00;       //内参基因阳性参考值
        #endregion

    public Flu_2019nCoV()
        {
            Type = EProjectType.AQ;
            TubesNo = new List<int>() { 1, 1, 1, 1, 1};
            ChannelsNo = new List<int>() { 1, 2, 3, 4, 5};
        }

        /// <summary>
        /// 判断逻辑
        /// </summary>
        /// <param name="samples">样本列表</param>
        /// <returns>非空：SLAN会弹出内容信息框，进行提示</returns>
        public override string Judge(List<Sample> samples)
        {
            //在结果判断之前，先检查是否有需要判断的样本
            if (samples.Count == 0) return "无判断样本！";

            int FAM = 0;
            int VIC = 1;
            int ROX = 2;
            int CY5 = 3;
            int Quasar705 = 4;
            int Negative_Control_Num = 0;        // 阴性质控数量
            int Positive_Control_Num = 0;        // 阳性质控数量
            bool Negative_Control_Pass = false;  // 阴性质控符合要求
            bool Positive_Control_Pass = false;  // 阳性质控符合要求
            bool Leak_Negative_Control = false;  // 缺少阴性质控
            bool Leak_Positive_Control = false;  // 缺少阳性质控

            /* ****************************************************************************************************************
             * 阴性质控(对照)的判读标准：（1）Flu-A, Flu-B, 2019nCoV全部阴性；******************************************************
             * **************************（2）内参Quasar705通道Ct小于等于IC_Positive_Cutoff *************************************
             * ****************************************************************************************************************
             */
            #region Negative Control 阴性质控(对照)的判读
            List<Sample> NegativeList = samples.Where(s => s.Type == SampleType.Negative).ToList();
            if(NegativeList.Count != 0)  //判断是否存在阴性质控
            {
                foreach (Sample sample in NegativeList)
                {
                    if (sample.Tubes[0].Targets[FAM].Result.Ct > FluA_Positive_Cutoff &&
                        sample.Tubes[0].Targets[VIC].Result.Ct > ORFab1_Positive_Cutoff &&
                        sample.Tubes[0].Targets[ROX].Result.Ct > FluB_Positive_Cutoff &&
                        sample.Tubes[0].Targets[CY5].Result.Ct > Ngene_Positive_Cutoff &&
                        sample.Tubes[0].Targets[Quasar705].Result.Ct <= IC_Positive_Cutoff)
                    {
                        Negative_Control_Num = Negative_Control_Num + 1;
                        sample.Conclusion.Add(new Conclusion() { Content = "阴性质控：符合要求" });
                    }
                    else
                    {
                        sample.Conclusion.Add(new Conclusion() { Content = "阴性质控：不符合要求" });
                    }
                }
                if (NegativeList.Count == Negative_Control_Num)
                {
                    Negative_Control_Pass = true;
                }
            }
            // 一次实验中未设置阴性指控则给予提示
            else
            {
                Negative_Control_Pass = true;
                Leak_Negative_Control = true;
            }
            #endregion

            /* ****************************************************************************************************************
             * 阳性质控判读标准：（1）Flu-A, Flu-B, 2019nCoV全部阳性； *************************************************************
             * ****************************************************************************************************************
             */
            #region Positive Control 阳性质控(对照)的判读
            List<Sample> PositiveList = samples.Where(s => s.Type == SampleType.HighPositive).ToList();
            if(PositiveList.Count != 0)  //判断是否存在阳性质控
            {
                foreach (Sample sample in PositiveList)
                {
                    #region 判断各通道Ct值是否符合要求
                    if( sample.Tubes[0].Targets[FAM].Result.Ct <= FluA_Positive_Cutoff &&
                        sample.Tubes[0].Targets[VIC].Result.Ct <= ORFab1_Positive_Cutoff &&
                        sample.Tubes[0].Targets[ROX].Result.Ct <= FluB_Positive_Cutoff &&
                        sample.Tubes[0].Targets[CY5].Result.Ct <= Ngene_Positive_Cutoff)
                    {
                        Positive_Control_Num = Positive_Control_Num + 1;
                        sample.Conclusion.Add(new Conclusion() { Content = "阳性质控：符合要求" });
                    }
                    else
                    {
                        sample.Conclusion.Add(new Conclusion() { Content = "阳性质控：不符合要求" });
                    }
                    #endregion
                }
                if(PositiveList.Count == Positive_Control_Num)
                {
                	Positive_Control_Pass = true;
                }
            }
            //在一次实验中未设置阳性质控，则给与提示
            else
            {
            	Positive_Control_Pass = true;
            	Leak_Positive_Control = true;
            }
            #endregion

            /* ****************************************************************************************************************
             * 1.甲型流感病毒阳性：FAM → Flu-A阳性; ******************************************************************************
             * 2.乙型流感病毒阳性：ROX → Flu-B阳性; ******************************************************************************
             * 3.2019nCoV阳性：  (1) VIC → ORFab1阳性, 且 CY5 → N基因阳性；*******************************************************
             * ****************（2）VIC → ORFab1阳性 或 CY5 → N基因阳性, 需要重新检测。********************************************
             * ****************************************************************************************************************
             */
            #region Unkonwn_Sample_Judge 待测样本结果判读
            List<Sample> UnknownList = samples.Where(s => s.Type == SampleType.Unknown).ToList();   //取出所有“待测样本”
            if(UnknownList.Count != 0)
            {
                foreach (Sample sample in UnknownList)
                {
                    // 定义并初始化中间变量（局部变量）
                    bool UnknowSample_IC_Cheeck_Pass = false;
                    string FluA_Coclusion_Str = "";
                    string FluB_Coclusion_Str = "";
                    string nCoV_Coclusion_Str = "";
                    string UnknowSample_Coclusion_Str = "";

                    /* ****************************************************************************************************************
                     * 1.甲型流感病毒阳性：FAM → Flu-A阳性; ******************************************************************************
                     * 2.乙型流感病毒阳性：ROX → Flu-B阳性; ******************************************************************************
                     * 3.2019nCoV阳性：  (1) VIC → ORFab1阳性, 且 CY5 → N基因阳性；*******************************************************
                     * ****************（2）VIC → ORFab1阳性 或 CY5 → N基因阳性, 需要重新检测。********************************************
                     * ****************************************************************************************************************
                     */
                    #region
                    // 反应孔内至少一种靶标有扩增，即该反应孔检测结果有效
                    if (sample.Tubes[0].Targets[FAM].Result.Ct <= FluA_Positive_Cutoff ||
                        sample.Tubes[0].Targets[ROX].Result.Ct <= FluB_Positive_Cutoff ||
                        sample.Tubes[0].Targets[VIC].Result.Ct <= ORFab1_Positive_Cutoff ||
                        sample.Tubes[0].Targets[CY5].Result.Ct <= Ngene_Positive_Cutoff ||
                        sample.Tubes[0].Targets[Quasar705].Result.Ct <= IC_Positive_Cutoff)
                    {
                        UnknowSample_IC_Cheeck_Pass = true;
                    }
                    else
                    {
                        FluA_Coclusion_Str = "甲型流感病毒：无结果";
                        FluB_Coclusion_Str = "乙型流感病毒：无结果";
                        nCoV_Coclusion_Str = "新型冠状病毒2019-nCoV：无结果";
                    }
                    // 在反应孔结果有效前提下，进行结果判读
                    if (UnknowSample_IC_Cheeck_Pass)
                    {
                        // 判读甲型流感病毒
                        if(sample.Tubes[0].Targets[FAM].Result.Ct <= FluA_Positive_Cutoff)
                        {
                            FluA_Coclusion_Str = "甲型流感病毒：阳性(+)";
                        }
                        else
                        {
                            FluA_Coclusion_Str = "甲型流感病毒：阴性(-)";
                        }
                        // 判读乙型流感病毒
                        if(sample.Tubes[0].Targets[ROX].Result.Ct <= FluB_Positive_Cutoff)
                        {
                            FluB_Coclusion_Str = "乙型流感病毒：阳性(+)";
                        }
                        else
                        {
                            FluB_Coclusion_Str = "乙型流感病毒：阴性(-)";
                        }
                        // 判读新冠2019nCoV
                        if(sample.Tubes[0].Targets[VIC].Result.Ct <= ORFab1_Positive_Cutoff ||
                           sample.Tubes[0].Targets[CY5].Result.Ct <= Ngene_Positive_Cutoff)
                        {
                            nCoV_Coclusion_Str = "新型冠状病毒2019-nCoV：阳性(+)";
                        }
                        else
                        {
                            nCoV_Coclusion_Str = "新型冠状病毒2019-nCoV：阴性(-)";
                        }
                    }
                    // 汇总待测样本最终检测结果
                    UnknowSample_Coclusion_Str = FluA_Coclusion_Str + "\n" + FluB_Coclusion_Str + "\n" + nCoV_Coclusion_Str;
                    sample.Conclusion.Add(new Conclusion() { Content = UnknowSample_Coclusion_Str });
                    #endregion
                }
            }
            #endregion

            /* ****************************************************************************************************************
             * 针对2019nCoV检测结果可疑的重测样本进行判读 *************************************************************************
             * 1.甲型流感病毒阳性：FAM → Flu-A阳性; ******************************************************************************
             * 2.乙型流感病毒阳性：ROX → Flu-B阳性; ******************************************************************************
             * 3.2019nCoV阳性：VIC → ORFab1阳性 或 CY5 → N基因阳性, 即判读为“新型冠状病毒2019-nCoV阳性”。****************************
             * ****************************************************************************************************************
             */
            #region Retest_Sample_Judge 重测样本结果判读
            List<Sample> RetestList = samples.Where(s => s.Type == SampleType.RetestSample).ToList();   //取出所有“重测样本”
            if (RetestList.Count != 0)
            {
                foreach (Sample sample in RetestList)
                {
                    // 定义并初始化中间变量（局部变量）
                    bool RetestSample_IC_Cheeck_Pass = false;
                    string FluA_Coclusion_Str = "";
                    string FluB_Coclusion_Str = "";
                    string nCoV_Coclusion_Str = "";
                    string RetestSample_Coclusion_Str = "";

                    /* ****************************************************************************************************************
                     * 针对2019nCoV检测结果可疑的重测样本进行判读 *************************************************************************
                     * 1.甲型流感病毒阳性：FAM → Flu-A阳性; ******************************************************************************
                     * 2.乙型流感病毒阳性：ROX → Flu-B阳性; ******************************************************************************
                     * 3.2019nCoV阳性：VIC → ORFab1阳性 或 CY5 → N基因阳性, 即判读为“新型冠状病毒2019-nCoV阳性”。****************************
                     * ****************************************************************************************************************
                     */
                    #region
                    // 反应孔内至少一种靶标有扩增，即该反应孔检测结果有效
                    if (sample.Tubes[0].Targets[FAM].Result.Ct <= FluA_Positive_Cutoff ||
                        sample.Tubes[0].Targets[ROX].Result.Ct <= FluB_Positive_Cutoff ||
                        sample.Tubes[0].Targets[VIC].Result.Ct <= ORFab1_Positive_Cutoff ||
                        sample.Tubes[0].Targets[CY5].Result.Ct <= Ngene_Positive_Cutoff ||
                        sample.Tubes[0].Targets[Quasar705].Result.Ct <= IC_Positive_Cutoff)
                    {
                        RetestSample_IC_Cheeck_Pass = true;
                    }
                    else
                    {
                        FluA_Coclusion_Str = "甲型流感病毒：无结果";
                        FluB_Coclusion_Str = "乙型流感病毒：无结果";
                        nCoV_Coclusion_Str = "新型冠状病毒2019-nCoV：无结果";
                    }
                    // 在反应孔结果有效前提下，进行结果判读
                    if (RetestSample_IC_Cheeck_Pass)
                    {
                        // 判读甲型流感病毒
                        if (sample.Tubes[0].Targets[FAM].Result.Ct <= FluA_Positive_Cutoff)
                        {
                            FluA_Coclusion_Str = "甲型流感病毒：阳性(+)";
                        }
                        else
                        {
                            FluA_Coclusion_Str = "甲型流感病毒：阴性(-)";
                        }
                        // 判读乙型流感病毒
                        if (sample.Tubes[0].Targets[ROX].Result.Ct <= FluB_Positive_Cutoff)
                        {
                            FluB_Coclusion_Str = "乙型流感病毒：阳性(+)";
                        }
                        else
                        {
                            FluB_Coclusion_Str = "乙型流感病毒：阴性(-)";
                        }
                        // 判读新冠2019nCoV
                        if (sample.Tubes[0].Targets[VIC].Result.Ct <= ORFab1_Positive_Cutoff ||
                            sample.Tubes[0].Targets[CY5].Result.Ct <= Ngene_Positive_Cutoff)
                        {
                            nCoV_Coclusion_Str = "新型冠状病毒2019-nCoV：阳性(+)";
                        }
                        else
                        {
                            nCoV_Coclusion_Str = "新型冠状病毒2019-nCoV：阴性(-)";
                        }
                    }
                    // 汇总待测样本最终检测结果
                    RetestSample_Coclusion_Str = FluA_Coclusion_Str + "\n" + FluB_Coclusion_Str + "\n" + nCoV_Coclusion_Str;
                    sample.Conclusion.Add(new Conclusion() { Content = RetestSample_Coclusion_Str });
                    #endregion
                }
            }
            #endregion

            /* ****************************************************************************************************************
             * 实验中未设置阴阳性质控不影响判读，仅给出弹窗提醒缺少质控 ************************************************************
             * 阴阳性质控不合格的，则给出提醒，但仍然进行判读 *********************************************************************
             * ****************************************************************************************************************
             */
            #region
            if (!Negative_Control_Pass || !Positive_Control_Pass)
            {
                MessageBox.Show("一个或多个阴阳性质控不符合要求！是否忽略？", "质控提示", MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
            }
            if(Leak_Negative_Control || Leak_Positive_Control)
            {
                MessageBox.Show("实验缺少阴性质控或阳性质控！是否忽略？", "质控提示", MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
            }
            #endregion

            return string.Empty;
        }
    }
}